/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.config;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.*;

import nfse.gui.client.NetFSEClient;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;  
import com.gwtext.client.widgets.MessageBox;  
import com.gwtext.client.widgets.Window;  
import com.gwtext.client.widgets.event.ButtonListenerAdapter;  

public class UserDialogBox extends Window {

    final TextBox userNameTextBox = new TextBox();

    final TextBox realNameTextBox = new TextBox();

    final CheckBox adminCheckBox = new CheckBox();

    final PasswordTextBox passwordTextBox1 = new PasswordTextBox();

    final PasswordTextBox passwordTextBox2 = new PasswordTextBox();

    boolean cancelled = false;

    boolean isNew = false;

    ConfigurationTab configurationTab = null;
    
    String userName  = null;
    
    public UserDialogBox(ConfigurationTab configTab) {
        final UserDialogBox note = this;
        this.configurationTab = configTab;
        // Set the dialog box's caption.
        FlexTable table = new FlexTable();
        // DialogBox is a SimplePanel, so you have to set it's widget property
        // to
        // whatever you want its contents to be.

        int idx = -1;

        this.setShadow(true);
        this.setFrame(true);
        //this.setWidth(300);
        //this.setHeight(200);
        
        
        
        this.setIconCls("pac-icon");
        
        table.setHTML(++idx, 0, "<b>User Name</b>");
        table.setWidget(idx, 1, userNameTextBox);
        table.setHTML(++idx, 0, "<b>Real Name</b>");
        table.setWidget(idx, 1, realNameTextBox);
        table.setHTML(++idx, 0, "<b>Admin</b>");
        table.setWidget(idx, 1, adminCheckBox);
        table.setHTML(++idx, 0, "<b>Password</b>");
        table.setWidget(idx, 1, passwordTextBox1);
        table.setHTML(++idx, 0, "<b>Confirm</b>");
        table.setWidget(idx, 1, passwordTextBox2);

        Button cancelButton = new Button();
        cancelButton.setText("Cancel");
        cancelButton.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject obj) {
                cancelled = true;
                note.hide();

            }
        });

        Button okButton = new Button();
        okButton.setText("OK");
        okButton.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject obj) {
                if (isNew && passwordTextBox1.getText().length() == 0 && passwordTextBox2.getText().length() == 0) {
                        MessageBox.alert("You must enter a password for the new user.");
                        return;
                    
                }
                
                if (passwordTextBox1.getText().length() > 0 && passwordTextBox2.getText().length() > 0) {
                    if (!passwordTextBox1.getText().equals(passwordTextBox2.getText())) {
                        MessageBox.alert("The passwords do not match. Please reneter the password values.");
                        return;
                    }
                }

                if (userNameTextBox.getText().trim().length() == 0) {
                    MessageBox.alert("Please enter a value in the User Name field.");
                    return;
                }

                if (realNameTextBox.getText().trim().length() == 0) {
                    MessageBox.alert("Please enter a value in the Real Name field.");
                    return;
                }
                
                UserInfo info = note.getUserInfo();
                if (note.isNew()) {
                    addUserInfo(info);
                } else {
                    editUserInfo(userName, info);
                }

                cancelled = false;
                note.hide();

            }
        });

        Button resetButton = new Button();
        resetButton.setText("Reset");
        resetButton.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject obj) {
                reset();
            }
        });

        this.addButton(resetButton);
        this.addButton(okButton);
        this.addButton(cancelButton);

        this.add(table);

    }

    private synchronized void addUserInfo(UserInfo userInfo) {

        ConfigServiceAsync nfseService = (ConfigServiceAsync) GWT.create(ConfigService.class);

        ServiceDefTarget endpoint = (ServiceDefTarget) nfseService;
        endpoint.setServiceEntryPoint(NetFSEClient.CONFIG_URL);

        AsyncCallback callback = new AsyncCallback() {
            public void onFailure(Throwable caught) {
                NetFSEClient.redirectToHome();
            }

            public void onSuccess(Object result) {
                configurationTab.users();
                NetFSEClient.showGenericDialog("User Add", (String) result);
            }
        };

        nfseService.addUser(userInfo, callback);

    }

    private synchronized void editUserInfo(String userName, UserInfo info) {

        ConfigServiceAsync nfseService = (ConfigServiceAsync) GWT.create(ConfigService.class);

        ServiceDefTarget endpoint = (ServiceDefTarget) nfseService;
        endpoint.setServiceEntryPoint(NetFSEClient.CONFIG_URL);

        AsyncCallback callback = new AsyncCallback() {
            public void onFailure(Throwable caught) {
                NetFSEClient.redirectToHome();
            }

            public void onSuccess(Object result) {
                configurationTab.users();
                NetFSEClient.showGenericDialog("User Edit", (String) result);
            }
        };

        nfseService.updateUser(userName, info, callback);

    }
    
    public UserInfo getUserInfo() {
        UserInfo userInfo = new UserInfo();
        userInfo.userName = userNameTextBox.getText().trim();
        userInfo.realName = realNameTextBox.getText().trim();
        userInfo.admin = adminCheckBox.isChecked();
        userInfo.password = passwordTextBox1.getText();
        return userInfo;
    }
    
    public String getPassword() {
        return passwordTextBox1.getText();
    }

    public void set(UserInfo info) {
        adminCheckBox.setChecked(info.admin);
        userNameTextBox.setText(info.userName);
        this.userName = info.userName;
        realNameTextBox.setText(info.realName);
        passwordTextBox1.setText("");
        passwordTextBox2.setText("");
    }

    public boolean isCancelled() {
        return cancelled;
    }

    public boolean isNew() {
        return isNew;
    }

    public void setNew(boolean isNew) {
        this.isNew = isNew;
        if (isNew)
            reset();
    }

    private void reset() {
        adminCheckBox.setChecked(false);
        userNameTextBox.setText("");
        realNameTextBox.setText("");
        passwordTextBox1.setText("");
        passwordTextBox2.setText("");
    }
}
